package com.wisdom.demo.mapper;

import com.wisdom.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 11951
 * Date: 2025-03-20
 * Time: 9:47
 */
@Mapper
public interface UserInfoMapper {
    //需求：根据字段查找
    @Select("select username,`password`,age,gender from user_info")
    public List<UserInfo> queryAllUser();
//    需求: 查找id=4的⽤⼾,对应的SQL就是: select * from user_info where id=4
    @Select("select * from user_info where id=#{id}")
    UserInfo queryById(Integer id);
    //@Param可以设置别名
    @Select("select * from user_info where id=#{userId}")
    UserInfo queryBuId2(@Param("userId") Integer id);
/*
* 增加
* */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age, gender, phone) " +
            "values  (#{userName},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);
    /*
    * 返回主键
     * */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, age, gender, phone) values (# {userInfo.username},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert1(@Param("userInfo") UserInfo userInfo);

    /*
    * 删
    * */
    @Delete("delete  from user_info where id=#{id}")
    void delete(Integer id);
    /*
    * 改
    * */
    @Update("update user_info set `password`=#{password} ,phone=#{phone} where id=#{id}")
    void update(UserInfo id);
    /*
    * 查
    * */
//    1.取别名法
    @Select("select id, username, `password`, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
    List<UserInfo> queryAllUser1();

    //2.结果映射
    @Select("select id, username, `password`, age, gender, phone, " +
            "delete_flag, create_time, update_time from user_info")
    @Results({
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    List<UserInfo> queryAllUser2();
    /*
    * mybatis自带的驼峰命名
    * */
    @Select("select * from user_info")
    List<UserInfo> queryAllUser3();
//先看Interger类型的参数
    @Select("select username, `password`, age, gender, phone from user_info where id= #{id} ")
            UserInfo queryById1(Integer id);
    @Select("select username, `password`, age, gender, phone from user_info where id= ${id} ")
    UserInfo queryById2(Integer id);
    //再看一下String类型
    @Select("select username, `password`, age, gender, phone from user_info where username= #{name} ")
            UserInfo queryByName(String name);
    @Select("select username, `password`, age, gender, phone from user_info where username= ${name} ")
    UserInfo queryByName2(String name);
    @Select("select username,`password`,age,gender,phone from user_info where username='${username}'")
    List<UserInfo> queryName(String name);

    @Select("select username, `password`, age, gender, phone from user_info " +
            "where username= '${name}' and password='${password}' ")
    List<UserInfo> queryUserByPassword(String name,String password);
@Select("select id, username, age, gender, phone, delete_flag, create_time, update_time " +
        "from user_info " +
        "order by id ${sort}")
    List<UserInfo>queryAllUserSort();

    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time " +
            "from user_info where username like concat('%',#{key},'%')")
    List<UserInfo> queryAllUserByLike(String key);


}
